package cn.dxs.gitee.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.util.Set;

import cn.dxs.gitee.App;

/**
 * @author lijian
 * @date 2019-09-21 8:43
 */
public class PrefUtils {

    public static final String LIGHT_INDIGO = "Light indigo";
    public static final String DARK = "Dark";
    public static final String LIGHT_TEAL = "Light teal";
    public static final String AMOLED_DARK = "AMOLED dark";

    public static final int LIGHT_BLUE = 0;
    public static final int BLUE = 1;
    public static final int INDIGO = 2;
    public static final int ORANGE = 3;

    public static final int YELLOW = 4;
    public static final int AMBER = 5;
    public static final int GREY = 6;
    public static final int BROWN = 7;

    public static final int CYAN = 8;
    public static final int TEAL = 9;
    public static final int LIME = 10;
    public static final int GREEN = 11;

    public static final int PINK = 12;
    public static final int RED = 13;
    public static final int PURPLE = 14;
    public static final int DEEP_PURPLE = 15;

    public static final String FIRST_USE = "firstUse";

    public static final String THEME = "appTheme";
    public static final String ACCENT_COLOR = "accentColor";
    public static final String START_PAGE = "startPage";
    public static final String CACHE_FIRST_ENABLE = "cacheFirstEnable";
    public static final String SYSTEM_DOWNLOADER = "systemDownloader";
    public static final String LANGUAGE = "language";
    public static final String LOGOUT = "logout";
    public static final String CODE_WRAP = "codeWrap";
    public static final String CUSTOM_TABS_ENABLE = "customTabsEnable";


    public static final String POP_TIMES = "popTimes";
    public static final String POP_VERSION_TIME = "popVersionTime";
    public static final String LAST_POP_TIME = "lastPopTime";

    public static final String NEW_YEAR_WISHES_TIP_ENABLE = "newYearWishesTipEnable";
    public static final String STAR_WISHES_TIP_TIMES = "starWishesTipFlag";
    public static final String LAST_STAR_WISHES_TIP_TIME = "lastStarWishesTipTime";

    public static final String DOUBLE_CLICK_TITLE_TIP_ABLE = "doubleClickTitleTipAble";
    public static final String ACTIVITY_LONG_CLICK_TIP_ABLE = "activityLongClickTipAble";
    public static final String RELEASES_LONG_CLICK_TIP_ABLE = "releasesLongClickTipAble";
    public static final String LANGUAGES_EDITOR_TIP_ABLE = "languagesEditorTipAble";
    public static final String COLLECTIONS_TIP_ABLE = "collectionsTipAble";
    public static final String BOOKMARKS_TIP_ABLE = "bookmarksTipAble";
    public static final String CUSTOM_TABS_TIPS_ENABLE = "customTabsTipsEnable";
    public static final String TOPICS_TIP_ABLE = "topicsTipAble";
    public static final String DISABLE_LOADING_IMAGE = "disableLoadingImage";


    public static final String SEARCH_RECORDS = "searchRecords";

    public static SharedPreferences getDefaultSp() {
        return PreferenceManager.getDefaultSharedPreferences(App.getContext());
    }

    public static void set(@NonNull String key, @NonNull Object value) {
        if (TextUtils.isEmpty(key) || value == null) {
            throw new NullPointerException(String.format("Key and value not be null key=%s, value=%s", key, value));
        }
        SharedPreferences.Editor edit = getDefaultSp().edit();
        if (value instanceof String) {
            edit.putString(key, (String) value);
        } else if (value instanceof Integer) {
            edit.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            edit.putLong(key, (Long) value);
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            edit.putFloat(key, (Float) value);
        } else if (value instanceof Set) {
            edit.putStringSet(key, (Set<String>) value);
        } else {
            throw new IllegalArgumentException(String.format("Type of value unsupported key=%s, value=%s", key, value));
        }
        edit.apply();
    }

    public static void clearKey(@NonNull String key) {
        getDefaultSp(App.getContext()).edit().remove(key).apply();
    }

    public static String getTheme() {
        return getDefaultSp(App.getContext()).getString(THEME, LIGHT_TEAL);
    }

    public static String getLanguage() {
        return getDefaultSp(App.getContext()).getString(LANGUAGE, "en");
    }

    public static String getStartPage() {
        return getDefaultSp(App.getContext()).getString(START_PAGE, "news");
    }

    public static int getAccentColor() {
        return getDefaultSp(App.getContext()).getInt(ACCENT_COLOR, 11);
    }

    public static boolean isCacheFirstEnable() {
        return getDefaultSp(App.getContext()).getBoolean(CACHE_FIRST_ENABLE, true);
    }

    public static boolean isCodeWrap() {
        return getDefaultSp(App.getContext()).getBoolean(CODE_WRAP, false);
    }

    public static boolean isDoubleClickTitleTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(DOUBLE_CLICK_TITLE_TIP_ABLE, true);
    }

    public static boolean isActivityLongClickTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(ACTIVITY_LONG_CLICK_TIP_ABLE, true);
    }

    public static boolean isReleasesLongClickTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(RELEASES_LONG_CLICK_TIP_ABLE, true);
    }

    public static boolean isLanguagesEditorTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(LANGUAGES_EDITOR_TIP_ABLE, true);
    }

    public static int getPopTimes() {
        return getDefaultSp(App.getContext()).getInt(POP_TIMES, 0);
    }

    public static long getPopVersionTime() {
        return getDefaultSp(App.getContext()).getLong(POP_VERSION_TIME, 1);
    }

    public static long getLastPopTime() {
        return getDefaultSp(App.getContext()).getLong(LAST_POP_TIME, 0);
    }

    /**
     * 标星提示时间
     *
     * @return
     */
    public static int getStarWishesTipTimes() {
        return getDefaultSp(App.getContext()).getInt(STAR_WISHES_TIP_TIMES, 0);
    }

    /**
     * 最后一颗星希望提示时间
     *
     * @return
     */
    public static long getLastStarWishesTipTime() {
        return getDefaultSp(App.getContext()).getLong(LAST_STAR_WISHES_TIP_TIME, 0);
    }

    public static boolean isSystemDownloader() {
        return getDefaultSp(App.getContext()).getBoolean(SYSTEM_DOWNLOADER, true);
    }

    public static boolean isCustomTabsEnable() {
        return getDefaultSp(App.getContext()).getBoolean(CUSTOM_TABS_ENABLE, true);
    }

    public static String getSearchRecords() {
        return getDefaultSp(App.getContext()).getString(SEARCH_RECORDS, null);
    }

    public static boolean isFirstUse() {
        return getDefaultSp(App.getContext()).getBoolean(FIRST_USE, true);
    }

    public static boolean isCollectionsTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(COLLECTIONS_TIP_ABLE, true);
    }

    public static boolean isBookmarksTipAble() {
        return getDefaultSp(App.getContext()).getBoolean(BOOKMARKS_TIP_ABLE, true);
    }

    public static boolean isCustomTabsTipsEnable() {
        return getDefaultSp(App.getContext()).getBoolean(CUSTOM_TABS_TIPS_ENABLE, true);
    }

    public static boolean isTopicsTipEnable() {
        return getDefaultSp(App.getContext()).getBoolean(TOPICS_TIP_ABLE, true);
    }

    public static boolean isDisableLoadingImage() {
        return getDefaultSp(App.getContext()).getBoolean(DISABLE_LOADING_IMAGE, false);
    }

    public static boolean isLoadImageEnable() {
        return NetHelper.INSTANCE.getNetStatus() == NetHelper.TYPE_WIFI || !PrefUtils.isDisableLoadingImage();
    }

    public static boolean isnewYearWishesTipEnable() {
        return getDefaultSp(App.getContext()).getBoolean(NEW_YEAR_WISHES_TIP_ENABLE, true);
    }

    public static SharedPreferences getDefaultSp(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static SharedPreferences getSP(Context context, String spName) {
        return context.getSharedPreferences(spName, Context.MODE_PRIVATE);
    }
}
